from typing import List

input = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]


def trap(input: List[int]) -> int:
    lm, rm, v = 0, 0, 0
    lms, rms = [], []

    for i in input:
        if i > lm:
            lm = i
        lms.append(lm)
    for j in input[::-1]:
        if j > rm:
            rm = j
        rms.append(rm)
    rms.reverse()

    for i in range(len(input)):
        v += max(0, min(lms[i], rms[i])-input[i])

    return v

print(trap(input))